package com.rong.juc_test.juc_p6_p7;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport等待唤醒案例
 */
public class LockSupportDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            //之前错误的先唤醒后等待 LockSupport也支持 先unpark会导致park方法无效 类似于告诉ETC提前购买通行证
            System.out.println(Thread.currentThread().getName() + "comein 准备等待" + System.currentTimeMillis());
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "被唤醒" + System.currentTimeMillis());
        },"t1");
        t1.start();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "comein 准备唤醒");
            LockSupport.unpark(t1); // 唤醒t1
        },"t2").start();
    }
}
